<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\CouponsRequest;
use App\Http\Resources\CouponsConfigResource;
use App\Http\Resources\CouponsResource;
use Illuminate\Http\Request;
use App\Models\Coupon;
use App\Models\CouponsConfig;
use Carbon\Carbon;
use App\Services\GoodsShopService;
class CouponsController extends Controller
{
    public function  index(Request $request){
        $number = $request->number;
        $type = $request->type;//优惠券类别  1普通正常优惠券  2已使用优惠券  3已过期优惠券
//        $where = [['is_used',"=",'0']];
        switch ($type)
        {
            case 1:
                $where = [
                  ['is_used',"=","0"],
                  ["end_date",">=",Carbon::now()->toDateString()]
                ];
                break;
            case 2:
                $where = [
                    ['is_used',"=","1"],
                ];
                break;
            case 3:
                $where = [
                    ['is_used',"=","0"],
                    ["end_date","<",Carbon::now()->toDateString()]
                ];
        }
//        $where = [
//            ['1',"=",'1'],
//        ];
        if ($number){
            CouponsResource::wrap("data");
            if (isset($where)){
                $coupons = $request->user()->coupons()->where($where)->orderBy("id","desc")->limit($number)->get();
            }else{
                $coupons = $request->user()->coupons()->limit($number)->orderBy("id","desc")->get();
            }

        }else{
            if (isset($where)){
                $coupons = $request->user()->coupons()->where($where)->orderBy("id","desc")->paginate();
            }else{
                $coupons = $request->user()->coupons()->orderBy("id","desc")->paginate();
            }

        }
        return CouponsResource::collection($coupons);
    }

    //兑换积分商城中的产品
    public function store(CouponsRequest $request,GoodsShopService $goodsShopService){
        $goodsShopService->goodsShop($request->user(),$request->goods_id);
        return response("兑换成功",200);
    }

    public function update(Request $request,Coupon $coupon){
        $this->authorize("update",$coupon);
        if ($coupon->is_passed){
            abort(412,"卡券已过期");
        }
        if ($coupon->is_used){
            abort(412,"卡券已核销");
        }
        $coupon->is_used = true;
        $coupon->checker = $request->user()->nickname;
        $coupon->save();
        $data = [
            "msg"=>"核销成功",
            "user_id"=>$coupon->user_id,
            "coupon_title"=>$coupon->title,
        ];
        return response()->json($data)->setStatusCode(200);
//        return response("核销成功",200);
    }
    //暂时无用了
    public function check_coupons(Request $request){
        $where['required_days'] = $request->user()->sin_in_day;
        $where['type'] = 1;
        $couponsConfig = CouponsConfig::where($where)->first();
        //有这个条件了。准备发放礼品券
        if ($couponsConfig){
            Coupon::addCoupon($request->user()->id,$couponsConfig->id);
            //看看下一阶段是啥礼品券
            unset($where['required_days']);
            $couponsConfigInfo = CouponsConfig::where($where)
                ->where("required_days",">",$request->user()->sin_in_day)
                ->orderby("id")
                ->first();
            return new CouponsConfigResource($couponsConfigInfo);
        }else{
            return response(null,200);
        }
    }

}
